package com.captjack.common.util.io.serializer;

import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

import java.util.HashMap;

/**
 * <p>
 * ----------------------------------------------------------------------------- <br>
 * project name ：enforce-app <br>
 * function：function。。。。。 <br>
 * description：description。。。。。 <br>
 * copyright : © 2011-2018 <br>
 * ----------------------------------------------------------------------------- <br>
 * change history <br>
 * <table width="432" border="1">
 * <tr>
 * <td>version</td>
 * <td>time</td>
 * <td>author</td>
 * <td>change</td>
 * </tr>
 * <tr>
 * <td>1.0.0</td>
 * <td>2018/9/18 16:02</td>
 * <td>Jack Sparrow</td>
 * <td>create</td>
 * </tr>
 * </table>
 * <br>
 *
 * @author Jack Sparrow
 * @version 1.0.0
 * @date 2018/9/18 16:02
 * package com.sinoiov.gghypt.enforcelaw
 */
public final class ProtostuffSchemaFactory {

    /**
     * SCHEMA_HASH_MAP
     */
    private static final HashMap<String, Schema> SCHEMA_HASH_MAP = new HashMap<String, Schema>();

    /**
     * @param clazz 类对象
     * @param <T>   范型
     * @return 实例
     */
    public static <T> Schema generateSchemaByClass(Class<T> clazz) {
        Schema schema;
        if ((schema = SCHEMA_HASH_MAP.get(clazz.getName())) != null) {
            return schema;
        } else {
            Schema tempSchema = RuntimeSchema.getSchema(clazz);
            SCHEMA_HASH_MAP.put(clazz.getName(), tempSchema);
            return tempSchema;
        }
    }

}
